package labuladong.第四章;

import java.util.Stack;

public class KuoHaoPiPei {
    public static void main(String[] args) {
        String s = "{}[]()]";
        System.out.println(isValid(s));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(char c : s.toCharArray()){
            if (c=='(' || c=='{' || c=='[') {
                stack.push(c);
            } else if (!stack.isEmpty() && leftOf(c) == stack.peek()) {
                stack.pop();
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }

    public static char leftOf(char c) {
        if(c == ')') return '(';
        if(c == '}') return '{';
        return '[';
    }
}
